﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Ledict.Core.Data;
using System.Data.Common;
using Ledict.ConditionExtract.Model;
using Ledict.ConditionExtract.Common;
namespace Ledict.ConditionExtract.DAL
{
    public class ArgumentDAL
    {
        private string DbName;
        public ArgumentDAL()
        {
            DbName = "JudgeDoc";
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string AT_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(*) from SYS_Argument");
            strSql.Append(" where AT_ID=@AT_ID ");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@AT_ID", DbType.String, AT_ID);

            if (Convert.ToInt32(oBase.ExecuteScalar(oCmd)) > 0)
                return true;
            else
                return false;
        }

        public decimal GetShowIndex()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select max(AM_ShowIndex)+1 from SYS_Argument ");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            return Convert.ToDecimal(oBase.ExecuteScalar(oCmd));

        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(SYS_Argument model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"insert into SYS_Argument(
			AM_ID,AT_ID,AM_Name,AM_Code,AM_ShowIndex)
			values (@AM_ID,@AT_ID,@AM_Name,@AM_Code,@AM_ShowIndex)");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@AM_ID", DbType.String, model.AM_ID);
            oBase.AddInParameter(oCmd, "@AT_ID", DbType.String, model.AT_ID);
            oBase.AddInParameter(oCmd, "@AM_Name", DbType.String, model.AM_Name);
            oBase.AddInParameter(oCmd, "@AM_Code", DbType.String, model.AM_Code);
            oBase.AddInParameter(oCmd, "@AM_ShowIndex", DbType.Decimal, model.AM_ShowIndex);

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string amId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SYS_Argument ");
            strSql.Append(" where AM_ID=@AM_ID");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@AM_ID", DbType.String, amId);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除一条数据,通过AT_ID
        /// </summary>
        public bool DeleteByAtId(string atId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SYS_Argument ");
            strSql.Append(" where AT_ID=@AT_ID");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@AT_ID", DbType.String, atId);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }



        #region add by zhangpeng

        /// <summary>
        /// 根据字典类型，获取字典列表
        /// </summary>
        /// <param name="code">字典类型编码</param>
        /// <returns></returns>
        public DataTable GetArgumentDtByType(string code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@" select a.AM_ID,a.AM_Name,a.AM_Code from [dbo].[SYS_Argument] as a 
                            inner join [dbo].[SYS_ArgumentType] as b 
                            on a.AT_ID = b.AT_ID and b.AT_Code = @code 
                            order by am_showIndex  ");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@code", DbType.String, code);
            var otB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (otB.Rows.Count > 0)
            {
                return otB;
            }
            return new DataTable();
        }


        /// <summary>
        /// 根据字典类型和字典名称，查看字典名称是否存在
        /// </summary>
        /// <param name="name">字典名称</param>
        /// <param name="typeCode">字典类型编码</param>
        /// <returns></returns>
        public bool IsExist(string name, string typeCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@" select count(1) from [dbo].[SYS_Argument] as a 
                           inner join [dbo].[SYS_ArgumentType] as b 
                           on a.AT_ID = b.AT_ID and b.AT_Code = @code
                           and a.AM_Name = @name");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@code", DbType.String, typeCode);
            oBase.AddInParameter(oCmd, "@name", DbType.String, name);
            if (Convert.ToInt32(oBase.ExecuteScalar(oCmd)) > 0)
                return true;
            else
                return false;
        }


        /// <summary>
        /// 修改字典值名称，通过Am_ID
        /// </summary>
        public bool UpdateName(SYS_Argument model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"  update [dbo].[SYS_Argument]
                             set AM_Name = @AM_Name
                             where AM_ID = @AM_ID");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@AM_ID", DbType.String, model.AM_ID);
            oBase.AddInParameter(oCmd, "@AM_Name", DbType.String, model.AM_Name);


            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        #endregion
        

    }
}
